package cn.com.xy.sms.sdk.ui.popu.widget;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.ui.popu.util.SmartSmsSdkUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ViewUtil;
import cn.com.xy.sms.sdk.util.StringUtils;

/**
 * 火车到达站选择弹出框
 */
public class TrainArriveSelectDialog {

    private JSONArray mTrainArray;
    private Context mContext;
    private int mCurrentTrainIndex;
    private List<View> mAllSelects;
    private OnBottomClick mOnBottomClick = null;
    private AlertDialog mAlertDialog;
    public static final int CONFIRM = 0;
    public static final int CANNEL = 1;
    public DialogParams mParams;
    private static final String TAG = "TrainArriveSelectDialog";
    private TrainAdapter mAdapter = null;
    private String mDepartCityStr;
    private int mCityPosDiv;

    public TrainArriveSelectDialog(JSONArray train_array, Context mContext, int mCurrentTrainIndex) {
        super();
        this.mTrainArray = train_array;
        this.mContext = mContext;
        this.mCurrentTrainIndex = mCurrentTrainIndex;
        mAllSelects = new ArrayList<View>();

        mParams = new DialogParams();
        mParams.mDefaultTitleName = "";
    }

    public TrainArriveSelectDialog(JSONArray train_array, Context context, int currentTrainIndex, int state, String arriveCity) {
        super();
        this.mTrainArray = train_array;
        this.mContext = context;
        this.mCurrentTrainIndex = currentTrainIndex;
        this.mDepartCityStr = arriveCity;
        mAllSelects = new ArrayList<View>();

        mParams = new DialogParams();
        mParams.mDefaultTitleName = "";
    }

    /* SMARTISAN-375 zhaoxiachao 20161231 start */
    public void showTrainArriveDialog(OnBottomClick click) {
        try {
            this.mOnBottomClick = click;
            if (mTrainArray != null && mTrainArray.length() > 0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle(mParams.mDefaultTitleName);
                builder.setNegativeButton(mContext.getString(R.string.duoqu_ui_part_cancel), new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mAlertDialog.dismiss();
                        if (mOnBottomClick != null) {
                            mOnBottomClick.Onclick(CANNEL, mCurrentTrainIndex);
                        }
                    }
                });
                
                View customView = LayoutInflater.from(mContext).inflate(R.layout.duoqu_select_train_arrive_list_dialog, null);
                builder.setCancelable(true);
                // bind adapter
                ListView rootLayout = (ListView) customView.findViewById(R.id.item_roots);
                mAdapter = new TrainAdapter();
                mCityPosDiv = mAdapter.getArriveCityPos(mDepartCityStr);
                rootLayout.setAdapter(mAdapter);
                rootLayout.setSelection(mCurrentTrainIndex < mCityPosDiv ? mCityPosDiv +1 : mCurrentTrainIndex);
                rootLayout.setOnItemClickListener(new OnItemSelectDialog());

                // create dialog
                builder.setView(customView);
                mAlertDialog = builder.create();
                mAlertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                mAlertDialog.show();
                Window window = mAlertDialog.getWindow();
                window.setGravity(Gravity.CENTER);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(lp);
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }
    /* SMARTISAN-375 zhaoxiachao 20161231 end */

    private class OnItemSelectDialog implements OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            mCurrentTrainIndex = position;
            RadioButton itemCheck = (RadioButton) view.findViewById(R.id.item_check);
            itemCheck.setChecked(true);
            mAdapter.notifyDataSetChanged();
            
            mAlertDialog.dismiss();
            if (mOnBottomClick != null) {
                mOnBottomClick.Onclick(CONFIRM, mCurrentTrainIndex);
            }
        }
    }

    public interface OnBottomClick {
        public void Onclick(int type, int select);
    }

    public class DialogParams {
        public String mDefaultTitleName;
        public String mSelectItemKey;
    }

    private class TrainAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mTrainArray != null ? mTrainArray.length() : 0;
        }

        @Override
        public Object getItem(int position) {
            Object resultObj = null;
            try {
                resultObj = mTrainArray.get(position);
            } catch (JSONException e) {
                SmartSmsSdkUtil.smartSdkExceptionLog(TAG, e);
            }
            return resultObj;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemChildView = View.inflate(mContext, R.layout.duoqu_list_train_arrive_items_content_part, null);
            JSONObject itemJson = (JSONObject) getItem(position);
            String name = itemJson.optString("name");
            TextView itemText = (TextView) itemChildView.findViewById(R.id.item_text);
            itemText.setText(name);

            if(position <= mCityPosDiv){
                itemChildView.setAlpha(0.5f);
                itemChildView.setClickable(true);
                itemChildView.setEnabled(true);
            }else{
                itemChildView.setAlpha(1f);
                itemChildView.setClickable(false);
                itemChildView.setEnabled(false);
            }
            RadioButton itemCheck = (RadioButton) itemChildView.findViewById(R.id.item_check);
            if(mCurrentTrainIndex < mCityPosDiv){
                itemCheck.setChecked(true);
                mCurrentTrainIndex = mCityPosDiv + 1;
            }else if (position == mCurrentTrainIndex) {
                itemCheck.setChecked(true);
            } else {
                itemCheck.setChecked(false);
            }
            mAllSelects.add(itemChildView);
            return itemChildView;
        }

        /**
         * 获取出发城市的pos
         * @param arriveCity
         * @return
         */
        public int getArriveCityPos(String arriveCity) {
            int pos = 0;
            if(StringUtils.isNull(arriveCity)){
                return pos;
            }
            try {
                JSONObject tempObj = null;
                for (int i = 0; i < mTrainArray.length(); i++) {
                    tempObj = mTrainArray.optJSONObject(i);
                    if (arriveCity.equals(tempObj.optString("name"))) {
                        pos = i;
                        break;
                    }
                }

            } catch (Throwable ex) {
                SmartSmsSdkUtil.smartSdkExceptionLog(TAG, ex);
            }
            return pos;
        }

    }

}
